//
//  ByUserIndexRegistrationVC.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/8.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByUserIndexRegistrationVC.h"

// api
#import "ByApiUserIndex.h"                  // 获取分院
#import "ByApiRegistration.h"               // 获取分院科室

// view
#import "ByViewRegistrationHeader.h"
#import "ByViewClassificationScroll.h"      // 分类滑动视图
#import "ByViewSubClassificationScroll.h"   // 子分类滑动视图

@interface ByUserIndexRegistrationVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) ByViewRegistrationHeader      *headerView;
@property (nonatomic, strong) ByViewClassificationScroll    *classScroll;
@property (nonatomic, strong) ByViewSubClassificationScroll *subClassScroll;

@property (nonatomic, strong) UIView                        *bgView;
@property (nonatomic, strong) UITableView                   *hospitalView;

@property (nonatomic, strong) NSMutableArray                *items;         // 分类数据
@property (nonatomic, strong) NSMutableArray                *hospitalAry;   // 医院数据
@end

@implementation ByUserIndexRegistrationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"挂号";
    [self addLeftBackBarButtonItem];
    [self createViews];
    [self fetchData];
}

- (void)createViews{
    self.headerView = [[ByViewRegistrationHeader alloc] init];
    [self.view addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        }else{
            make.top.equalTo(self.view.mas_top);
        }
        make.left.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50));
    }];
    __weak typeof(self) weakSelf = self;
    CGFloat width = [@"鞍山金普医院" getSizeOfString:14].width + 80;
    self.headerView.switchBlock = ^(ByModelIndexBranch *model) {
        if (weakSelf.hospitalAry.count != 0) {
            weakSelf.hospitalView.hidden = NO;
            [weakSelf.hospitalView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(@(0));
                make.size.mas_equalTo(CGSizeMake(width,30 * (self.hospitalAry.count - 1)));
                make.top.equalTo(weakSelf.headerView.mas_bottom);
            }];
            weakSelf.bgView.hidden = NO;
            [weakSelf.hospitalView reloadData];
        }
    };
    
    self.classScroll.hidden = YES;
    self.subClassScroll.hidden = YES;
    self.bgView.hidden = YES;
    self.hospitalView.hidden = YES;
}

#pragma mark - getter
- (ByViewClassificationScroll *)classScroll {
    if (!_classScroll) {
        CGFloat width = [@"神经科科科科" getSizeOfString:14].width + 40;
        _classScroll = [[ByViewClassificationScroll alloc] init];
        [self.view addSubview:_classScroll];
        [_classScroll mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.headerView.mas_bottom);
            make.left.equalTo(@(0));
            make.width.equalTo(@(width));
            if (@available(iOS 11.0,*)) {
                make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
            }else{
                make.bottom.equalTo(self.view.mas_bottom);
            }
        }];
    }
    return _classScroll;
}
- (ByViewSubClassificationScroll *)subClassScroll {
    if (!_subClassScroll) {
        _subClassScroll = [[ByViewSubClassificationScroll alloc] init];
        [self.view addSubview:_subClassScroll];
        [_subClassScroll mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.classScroll.mas_right);
            make.top.equalTo(self.classScroll.mas_top);
            make.right.equalTo(@(0));
            make.bottom.equalTo(self.classScroll.mas_bottom);
        }];
    }
    return _subClassScroll;
}

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        [self.view addSubview:_bgView];
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.headerView.mas_bottom);
            make.left.equalTo(@(0));
            make.right.equalTo(@(0));
            make.bottom.equalTo(self.subClassScroll.mas_bottom);
        }];
        _bgView.hidden = YES;
        _bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        _bgView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideBgView)];
        [_bgView addGestureRecognizer:tap];
    }
    return _bgView;
}

- (UITableView *)hospitalView{
    if (!_hospitalView) {
        _hospitalView = [[UITableView alloc] init];
        [self.view addSubview:_hospitalView];
        CGFloat width = [@"鞍山金普医院" getSizeOfString:14].width + 80;
        [_hospitalView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@(0));
            make.top.equalTo(self.headerView.mas_top);
            make.size.mas_equalTo(CGSizeMake(width, 0));
        }];
        _hospitalView.delegate = self;
        _hospitalView.dataSource = self;
    }
    return _hospitalView;
}

- (NSMutableArray *)items{
    if (!_items) {
        _items = [NSMutableArray array];
    }
    return _items;
}

- (NSMutableArray *)hospitalAry{
    if (!_hospitalAry) {
        _hospitalAry = [NSMutableArray array];
    }
    return _hospitalAry;
}
#pragma mark - 获取数据
- (void)fetchData{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    [ByCoreToolAlert showLoadind:@"加载中"];
    [ByApiUserIndex getBranchList:model success:^(ByNetBaseResult *result) {
        [self.hospitalAry removeAllObjects];
        for (ByModelIndexBranch *model in result.data[@"list"]) {
            [self.hospitalAry addObject:model];
        }
        if (self.hospitalAry.count != 0) {
            if (self.branch_model) {
                self.headerView.model = self.branch_model;
                [self fetchBranchOffice:self.branch_model];
            }else{
                self.headerView.model = self.hospitalAry[0];
                [self fetchBranchOffice:self.hospitalAry[0]];
            }
        }

    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
    
}

// 获取分院科室
- (void)fetchBranchOffice:(ByModelIndexBranch *) model{
    ByNetParamsModel *param  = [[ByNetParamsModel alloc]  init];
    param.hid = model.branch_id;
    [ByApiRegistration getBranchOfficeList:param success:^(ByNetBaseResult *result) {
        [self.items removeAllObjects];
        for (ByModelRegistrationSubject *subject in result.data) {
            [self.items addObject:subject];
        }
        if (self.items.count != 0 ) {
            self.classScroll.classModels = [self.items copy];
            [self.classScroll reloadData];
            ByModelRegistrationSubject *first_model = self.items[0];
            [self getProductList:first_model];
            self.classScroll.selectingIndex = 0;
            [self setScrollClickBlock];
            self.classScroll.hidden = NO;

        }
        [ByCoreToolAlert hideHud];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showOnlyText:result.msg];
    }];
    
}

// 列表
- (void)getProductList:(ByModelRegistrationSubject *)model{
    NSMutableArray *temp = [NSMutableArray array];
    for (ByModelRegistrationSubject *sub_model in model.children) {
        [temp addObject:sub_model];
    }
    if (temp.count != 0) {
        self.subClassScroll.hidden = NO;
        self.subClassScroll.itemModels = [temp copy];
        [self.subClassScroll reloadData];
    }else{
        self.subClassScroll.hidden = YES;
    }
    
}
#pragma mark - 设置点击回调
- (void)setScrollClickBlock {
    __weak typeof(self) weakself = self;
    [self.classScroll setSelectBlock:^(ByModelRegistrationSubject *model) {
        [weakself getProductList:model];
    }];
    
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.hospitalAry.count - 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifer = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font = FONT(14);
    }
    // 过滤
    NSMutableArray *tempAry = [self.hospitalAry mutableCopy];
    [tempAry removeObject:self.headerView.model];
    ByModelIndexBranch *model = tempAry[indexPath.row];
    cell.textLabel.text = model.name;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *tempAry = [self.hospitalAry mutableCopy];
    [tempAry removeObject:self.headerView.model];
    self.headerView.model = tempAry[indexPath.row];
    self.hospitalView.hidden = YES;
    self.bgView.hidden = YES;
    // 刷新数据
    [self fetchBranchOffice:self.headerView.model];
}

#pragma mark - 隐藏
- (void)hideBgView{
    self.hospitalView.hidden = YES;
    self.bgView.hidden = YES;
}
@end
